package com.hzjx.fenci.service;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hzjx.fenci.model.SingleWord;


/**
 * 使用Hibernate来实现的一个SingleWord的单字处理类
 * 
 * @author user
 *
 */

@Service("singleWordService")
@Transactional
public class SingleWordServiceHibernate implements SingleWordService {
	
	
	@Autowired
	private SessionFactory sessionFactory;

	
	/**
	 * 获得Hibenrate当前Session
	 * @return
	 */
	protected Session getCurrentSession() {
		return this.sessionFactory.getCurrentSession();
	}

	@Transactional(readOnly=true)
	public List<SingleWord> getAll() {
		
		String hql="from SingleWord";
		Query q=this.getCurrentSession().createQuery(hql);
		return q.list();
	}

	public void update(SingleWord sw) {
		this.getCurrentSession().saveOrUpdate(sw);
		
	}

	public SingleWord getObject(String idstr) {
		Long id=Long.parseLong(idstr);
		SingleWord sw=(SingleWord)this.getCurrentSession().get(SingleWord.class, id);
		return sw;
	}

	public void del(String id) {
		SingleWord sw = this.getObject(id);
		if(sw!=null) this.getCurrentSession().delete(sw);
	}


	
	
	
	
}
